
package com.shop.cloud.upms.admin.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.common.core.util.EnvUtils;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.core.util.SensitiveUtils;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.common.nacos.util.CommentUtils;
import com.shop.cloud.common.nacos.util.NacosConfigUtils;
import com.shop.cloud.common.security.thirdparty.config.QQConfig;
import com.shop.cloud.common.security.thirdparty.config.ThirdPartyConfigProperties;
import com.shop.cloud.common.security.thirdparty.config.WXConfig;
import com.shop.cloud.upms.common.entity.ThirdpartyConfig;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.Yaml;

import java.util.HashMap;
import java.util.Map;

/**
 * 第三方登录配置
 *
 * @author
 * @date 2020-11-24 14:44:15
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/thirdpartyconfig")
@Tag(description = "thirdpartyConfig", name = "第三方登录配置管理")
public class ThirdpartyConfigController {

	private NacosConfigUtils nacosConfigUtils;

    /**
     * 第三方登录配置查询
     * @param
     * @return R
     */
    @Operation(summary = "第三方登录配置查询")
    @GetMapping
    @PreAuthorize("@ato.hasAuthority('sys:thirdpartyconfig:get')")
    public R getBy() throws NacosException {
		ConfigService configService = nacosConfigUtils.getConfigService();
		String content = configService.getConfig(EnvUtils.getConfigDataId(CommonConstants.CONFIG_DATA_ID_APPLICATION), CommonConstants.CONFIG_GROUP, CommonConstants.CONFIG_TIMEOUT_MS);
		Yaml yaml = new Yaml();
		Map<String,Object> confMap= yaml.load(content);
		Object object = confMap.get("thirdparty");
		JSONObject jSONObject = JSONUtil.parseObj(object);
		ThirdPartyConfigProperties thirdPartyConfigProperties = jSONObject.toBean(ThirdPartyConfigProperties.class);
		ThirdpartyConfig thirdpartyConfig = new ThirdpartyConfig();
		thirdpartyConfig.setQqAppId(thirdPartyConfigProperties.getQq().getAppId());
		thirdpartyConfig.setQqAppKey(thirdPartyConfigProperties.getQq().getAppKey());
		thirdpartyConfig.setWxAppId(thirdPartyConfigProperties.getWx().getAppId());
		thirdpartyConfig.setWxAppSecret(thirdPartyConfigProperties.getWx().getAppSecret());
		//数据脱敏
		String sensitiveQqAppKey = SensitiveUtils.process(thirdPartyConfigProperties.getQq().getAppKey());
		thirdpartyConfig.setQqAppKey(sensitiveQqAppKey);
		String sensitiveWxAppSecret = SensitiveUtils.process(thirdPartyConfigProperties.getWx().getAppSecret());
		thirdpartyConfig.setWxAppSecret(sensitiveWxAppSecret);
		return R.ok(thirdpartyConfig);
    }

    /**
     * 第三方登录配置修改
     * @param thirdpartyConfig 第三方登录配置
     * @return R
     */
    @Operation(summary = "第三方登录配置修改")
    @SysLog("修改第三方登录配置")
    @PutMapping
    @PreAuthorize("@ato.hasAuthority('sys:thirdpartyconfig:edit')")
    public R updateById(@RequestBody ThirdpartyConfig thirdpartyConfig) throws NacosException {
		ConfigService configService = nacosConfigUtils.getConfigService();
		String content = configService.getConfig(EnvUtils.getConfigDataId(CommonConstants.CONFIG_DATA_ID_APPLICATION), CommonConstants.CONFIG_GROUP, CommonConstants.CONFIG_TIMEOUT_MS);
		// 记录 yaml 文件的注释信息
		CommentUtils.CommentHolder holder = CommentUtils.buildCommentHolder(content);
		Yaml yaml = new Yaml();
		Map<String,Map<String,Map>> confMap= yaml.load(content);

		String qqAppKey = confMap.get("thirdparty").get("qq").get("appKey")+"";
		String sensitiveQqAppKey = SensitiveUtils.process(qqAppKey);
		if(!sensitiveQqAppKey.equals(thirdpartyConfig.getQqAppKey())){
			qqAppKey = thirdpartyConfig.getQqAppKey();
		}
		String wxAppSecret = confMap.get("thirdparty").get("wx").get("appSecret")+"";
		String sensitiveWxAppSecret = SensitiveUtils.process(wxAppSecret);
		if(!sensitiveWxAppSecret.equals(thirdpartyConfig.getWxAppSecret())){
			wxAppSecret = thirdpartyConfig.getWxAppSecret();
		}

		Map thirdpartyMap = new HashMap();
		QQConfig qQConfig = new QQConfig();
		qQConfig.setAppId(thirdpartyConfig.getQqAppId());
		qQConfig.setAppKey(qqAppKey);
		WXConfig wXConfig = new WXConfig();
		wXConfig.setAppId(thirdpartyConfig.getWxAppId());
		wXConfig.setAppSecret(wxAppSecret);
		thirdpartyMap.put("qq",JSONUtil.parseObj(qQConfig));
		thirdpartyMap.put("wx",JSONUtil.parseObj(wXConfig));
		confMap.put("thirdparty",thirdpartyMap);
		String confStr = yaml.dumpAsMap(confMap);
		// 填充注释信息
		confStr = holder.fillComments(confStr);
		boolean rs = configService.publishConfig(EnvUtils.getConfigDataId(CommonConstants.CONFIG_DATA_ID_APPLICATION), CommonConstants.CONFIG_GROUP,confStr,"yaml");
        return R.ok(rs);
    }

}
